package com.tbit.main.controller;

import cn.hutool.core.date.DateUtil;
import com.tbit.main.common.JsonResult;
import com.tbit.main.exception.BaseException;
import com.tbit.main.pojo.AccountUser;
import com.tbit.main.pojo.VO.AccountDataMainVO;
import com.tbit.main.pojo.VO.AccountOperationalDataVO;
import com.tbit.main.service.AccountDayStatService;
import com.tbit.main.service.AccountService;
import com.tbit.main.service.DataService;
import com.tbit.main.service.TokenService;
import com.tbit.main.service.manager.util.DateUtils;
import com.tbit.main.util.DateTimeUtil;
import com.tbit.main.util.ListUtil;
import com.tbit.main.util.LogUtil;
import com.tbit.main.util.StringUtil;
import com.tbit.manager.api.AccountDayStatApi;
import com.tbit.manager.api.dto.OperationalDataDTO;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 运营简报3.5
 *
 * @author LMY
 * @create 2022-11-18 11:25
 */
@RestController
@RequestMapping("/accountDayStat")
public class AccountDayStatController {

    @Autowired
    private AccountService accountService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private AccountDayStatService accountDayStatService;
    @Autowired
    private DataService dataService;
    @Autowired
    private AccountDayStatApi accountDayStatApi;

    /**
     * 资金数据
     *
     * @return
     */
    @GetMapping("/operationalDataToMain")
    public JsonResult operationalDataToMain(String token, String accountIds, String startTime, String endTime) {
        //获取用户
        AccountUser accountUser = tokenService.getAccount(token);
        //获取品牌
        Integer brandId = accountService.getByAccountAndType(accountUser.getAccountId(), 1).getAccountId();
        //判断是否使用缓存数据
        String cacheTime = "2018-01-01 00:00:00";
        String dayToEnd = " 23:59:59";
        if (Objects.equals(cacheTime, startTime) && endTime.equals(DateTimeUtil.getNowTimeYMD() + dayToEnd)) {
            //获取所有品牌id,区域id
            Map<Integer, List<Integer>> brandIdAccountIdS = accountService.getStageBrandIdAccountIdS();
            if (brandIdAccountIdS.containsKey(brandId)) {
                List<Integer> accountIdIntegerS = StringUtil.getIntegerList(accountIds);
                List<Integer> listInteger = ListUtil.getSameIntegerList(brandIdAccountIdS.get(brandId), accountIdIntegerS);
                //查询所有区域
                if (CollectionUtils.isEmpty(listInteger)) {
                    LogUtil.info("缓存数据！");
                    return JsonResult.succ(dataService.getoperationalDataToMainMapTemp(brandId));
                }
            }
        }
        //请求客户端接口
        Map<String, Object> operationalDataToMain = accountDayStatApi.operationalData(new OperationalDataDTO(DateUtil.parse(startTime), DateUtil.parse(endTime), brandId, accountIds));
        if (operationalDataToMain == null) {
            throw new BaseException("System.error");
        }
        return JsonResult.succ(operationalDataToMain);
    }

    @GetMapping("/groupMachineByDay")
    public JsonResult<List<AccountDataMainVO>> groupMachineByDay(String token, String accountIds, String startTime, String endTime) {
        //获取登录用户
        AccountUser accountUser = tokenService.getAccount(token);
        // 获取品牌
        Integer brandId = accountService.getByAccountAndType(accountUser.getAccountId(), 1).getAccountId();

        return accountDayStatService.groupMachineByDay(brandId, accountIds, startTime, endTime);
    }

    @GetMapping("/groupOperationalDataByDay")
    public JsonResult<List<AccountOperationalDataVO>> groupOperationalDataByDay(String token, String accountIds, String startTime, String endTime) {
        //获取登录用户
        AccountUser accountUser = tokenService.getAccount(token);
        // 获取品牌
        Integer brandId = accountService.getByAccountAndType(accountUser.getAccountId(), 1).getAccountId();

        List<String> dateList = DateUtils.getMonthBetweenDate(startTime, endTime, "yyyy-MM-dd", Calendar.DAY_OF_MONTH);
        List<AccountOperationalDataVO> collect = dateList.stream().map(x -> {
            Map<String, Object> dateMap = accountDayStatApi.operationalData(new OperationalDataDTO(DateUtil.parse(x + " 00:00:00"), DateUtil.parse(x + " 23:59:59"), brandId, accountIds));
            //Map<String, Object> dateMap = UQHttpManager.operationalDataToMain(brandId, accountIds, x + " 00:00:00", x + " 23:59:59");
            if (dateMap == null) {
                dateMap = Collections.emptyMap();
            }
            return new AccountOperationalDataVO(x, dateMap);
        }).collect(Collectors.toList());
        return JsonResult.succ(collect);
    }


    /**
     * 运维数据
     */
    @RequestMapping("/getByAccountId")
    public JsonResult getMain(String token, String accountIds, String startTime, String endTime) {
        //获取登录用户
        AccountUser accountUser = tokenService.getAccount(token);
        // 获取品牌
        Integer brandId = accountService.getByAccountAndType(accountUser.getAccountId(), 1).getAccountId();
        return JsonResult.succ(accountDayStatService.getMachine(brandId, accountIds, startTime, endTime));
    }

    /**
     * 友盟数据
     */
    @RequestMapping("/getByAccountIdUM")
    public JsonResult getByAccountIdUM(String token, String startTime, String endTime) {
        //获取用户
        AccountUser accountUser = tokenService.getAccount(token);
        //获取品牌
        Integer brandId = accountService.getByAccountAndType(accountUser.getAccountId(), 1).getAccountId();

        return JsonResult.succ(accountDayStatService.getByAccountIdUM(brandId, startTime, endTime));
    }

}

